package org.example.diet_compass.mapper;

import org.apache.ibatis.annotations.*;
import org.example.diet_compass.domain.Cart;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.Map;

/**
* @author 86135
* @description 针对表【cart(存储购物车信息的表)】的数据库操作Mapper
* @createDate 2025-03-16 15:01:09
* @Entity org.example.diet_compass.domain.Cart
*/
public interface CartMapper extends BaseMapper<Cart> {
    @Select("SELECT good_id as id , `name`," +
            "`count`,price,picture,selected,description FROM `cart` c left join goods g on c.good_id = g.id where user_id = #{userId}  ")
    List<Map<String, Object>> getGoodsByUserId(Integer userId);

    @Delete("delete from `cart` where user_id = #{userId} and good_id = #{goodId}")
    void removeByUserIdAndGoodId(Integer userId, String goodId);

    @Update("update `cart` set `count`=#{count} where user_id = #{userId} and good_id = #{goodId}")
    void updateCount(Integer userId,String goodId,Integer count);

    @Update("update `cart` set selected = #{selected} where user_id = #{userId} and good_id = #{goodId} ")
    void updateSelected(Integer userId, String goodId, int selected);

    @Update("update  `cart` set selected = #{selected} where user_id=#{userId}")
    void updateAllGoods( Integer userId,Integer selected);

    @Select("select * from `cart` where user_id=#{userId} and selected = 1")
    List<Cart> getGoodsByUserIdAndSelected(Integer userId);

    @Delete("delete from `cart` where user_id = #{userId} and selected = 1")
    void removeByUserIdAndSelected(Integer userId);

    @Select("select * from cart where user_id=#{userId} and good_id = #{goodId}")
    Cart getCartGoodByUserIdAndGoodId(int userId, String goodId);

    @Insert("<script> insert into cart (cart_id,user_id,good_id,selected,`count`) values " +
            "<foreach collection='goodMapList' item='goodMap' separator = ','>" +
            "(null,#{userId},#{goodMap.goods_id},1,#{goodMap.quantity})"+
            "</foreach></script> ")
    void insertCartBatchByGoodIdsAndUserId(@Param("userId") int userId, @Param("goodMapList") List<Map<String, Object>> goodIds);
}




